$(function(){
  
  renderShop();

  renderArea();

  renderPro();
 
  var shopid;
  var areaid;

  //  点击京东,切换

  $('.mmm_nav .nav_shop').on('click',function(){
    // 区域隐藏
    $('.mmm_nav .area').addClass('hide');
    $('.mmm_nav .nav_area span').addClass('fa fa-caret-up');
    //点击切换隐藏
    $('.mmm_nav .shop').toggleClass('hide');
    $('.mmm_nav .nav_shop span').toggleClass('fa fa-caret-down').toggleClass('fa fa-caret-up');
    //  选择功能
    $('.mmm_nav .shop li').each(function(i,e){
      $(this).on('click',function(){
        var tex = $(this).children().text();
            shopid = $(this).data('shopid');
            console.log(shopid);
        $('.nav_shop em').text( tex );
        $('.mmm_nav .shop').addClass('hide');
        $('.mmm_nav .nav_shop span').addClass('fa fa-caret-up');

        renderPro(shopid,areaid);
      })
    })
  })

  // 点击华南,切换
  

  $('.mmm_nav .nav_area').on('click',function(){
    // 店铺隐藏
    $('.mmm_nav .shop').addClass('hide');
    $('.mmm_nav .nav_shop span').addClass('fa fa-caret-up');
    //点击切换隐藏
    $('.mmm_nav .area').toggleClass('hide');
    $('.mmm_nav .nav_area span').toggleClass('fa fa-caret-down').toggleClass('fa fa-caret-up');

    //  选择功能
    $('.mmm_nav .area li').each(function(i,e){
      $(this).on('click',function(){
        var tex = $(this).children().text();
            tex = tex.slice(0,2);
            areaid  = $(this).data('areaid');
            console.log(areaid);
        $('.nav_area em').text( tex );
        $('.mmm_nav .area').addClass('hide');
        $('.mmm_nav .nav_area span').addClass('fa fa-caret-up');
        
        renderPro(shopid,areaid);
      })
    })
  })




  // 获取店铺信息
  function renderShop(){
    $.ajax({
      url: 'http://127.0.0.1:9090/api/getgsshop',
      type: 'get',
      success: function(info){
        console.log(info);
        $('.mmm_nav .shop ul').html( template('tmp',info) );
        
        
      }
    })

  }


  //获取区域信息
  function renderArea(){
    $.ajax({
      url: 'http://127.0.0.1:9090/api/getgsshoparea',
      type: 'get',
      success: function(info){
        console.log(info);
        $('.mmm_nav .area ul').html( template('tmp2',info) );
      }
    })
  }


  // 获取该店铺该区域的商品列表信息
  function renderPro(shopid,areaid){
    $.ajax({
      url: 'http://127.0.0.1:9090/api/getgsproduct',
      type: 'get',
      data: {
        shopid: shopid || 0,
        areaid: areaid || 0
      },
      success: function(info){
        console.log(info);
        $('.mmm_main').html( template('tmp3',info) );
      }
    })
  }

})